
package com.bear.cmcc.NetTestHTTP.ui.adapter;

import com.bear.cmcc.NetTestHTTP.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 测试结果详细信息中小区信息适配器。
 * 
 * @author suchangjun
 */
public class DetailAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;

    private ArrayList<String[]> list;

    private ViewHolder viewholder;

    /**
     * 初始化适配器。
     * @param context
     * @param list 这个值一定不能为null
     */
    public DetailAdapter(Context context, ArrayList<String[]> list) {
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        String[] csb = list.get(position);
        viewholder = null;
        if (view == null) {
            viewholder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.activity_detail_item, null);
            viewholder.showTitle = (TextView) view.findViewById(R.id.tv_detail_title);
            viewholder.showValue = (TextView) view.findViewById(R.id.tv_detail_value);
            view.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) view.getTag();
        }
        if (csb != null) {
            viewholder.showTitle.setText(csb[0]);
            viewholder.showValue.setText(csb[1]);
        }
        return view;
    }

    class ViewHolder {

        TextView showTitle;

        TextView showValue;

    }

    public void clearData() {
        if (list != null) {
            list.clear();
            notifyDataSetChanged();
        }
    }

    public void addData(ArrayList<String[]> item) {
        if (list != null && item != null && item.size() > 0) {
            list.addAll(item); 
            notifyDataSetChanged();
        }
    }

}
